package src.main.java.org.reflect.demo;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ObjectFrame {
    //保存任意对象字段到文件中
    public static void saveObject(Object o) throws Exception {
        //打印流
        PrintStream printStream = new PrintStream(new FileOutputStream("E:\\JavaLearn\\javaCode\\src\\data.txt",true));
        //获取class对象
        Class aClass = o.getClass();
        String simpleName = aClass.getSimpleName();
        printStream.println("---------"+simpleName+"-----------");
        //从类中提取全部成员变量
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            //拿到成员变量的名字
            String name = declaredField.getName();
            declaredField.setAccessible(true);
            //拿到这个成员变量在对象中的数据
            String o1 = declaredField.get(o)+"";
            printStream.println(name+"--"+o1);
        }
        printStream.close();
    }
}
